package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.media.pojos.WmUser;
import com.heima.search.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Override
    public ResponseResult update(WmUser wmUser) {
        //判断参数
        if (wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //根据WmUser储存
        this.save(wmUser);
        return ResponseResult.okResult();
    }

    @Override
    public WmUser findByName(String name) {
        //判断参数
        if (name==null){
            return null;
        }
        //创建查询条件
        LambdaQueryWrapper<WmUser> queryWrapper = Wrappers.<WmUser>lambdaQuery();
        queryWrapper.eq(WmUser::getName, name);
        List<WmUser> list = this.list(queryWrapper);
        //判断返回值是否为空
        if (list!=null&&!list.isEmpty()){
            return list.get(0);
        }
        return null;

    }

    @Override
    public ResponseResult login(WmUserDto dto) {
        //判断参数
        if (dto==null|| StringUtils.isEmpty(dto.getName())||StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        }
        //根据姓名去数据库中查询

        WmUser wmUser = this.getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, dto.getName()));
        if (wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该用户不存在");
        }
        //获取用户密码所对应的盐
        String salt = wmUser.getSalt();

        //根据盐和密码通过MD5加密与数据库比较
        String password = DigestUtils.md5DigestAsHex((dto.getPassword() + salt).getBytes());
        if (!wmUser.getPassword().equals(password)){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //根据AppJwtUtil工具类获取token
        String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
        //将token和user放在map集合中
        Map<String,Object> map = new HashMap();
        //将盐和密码这些敏感信息覆盖
        wmUser.setPassword("");
        wmUser.setSalt("");
        map.put("user",wmUser);
        map.put("token",token);

        return ResponseResult.okResult(map);
    }
}
